# 例子 3: 自定义的迭代器
class MyRange:
    def __init__(self, end):
        self.current = 0
        self.end = end

    def __iter__(self):
        return self

    def __next__(self):
        if self.current < self.end:
            value = self.current
            self.current += 1
            return value
        else:
            raise StopIteration

            # 使用内置的for循环，它依赖于对象的__iter__和__next__方法


for num in MyRange(5):
    print(num)  # 输出: 0 1 2 3 4

